<template>
  <div>
    <van-nav-bar left-arrow title="个人信息" @click-left="$router.back()" />

    <input type="file" hidden ref="file" @change="onFileChange" />
    <van-cell title="头像" is-link @click="$refs.file.click()">
      <van-image class="avatar" :src="userInfo.photo" fit="cover" round />
    </van-cell>

    <van-cell
      title="昵称"
      :value="userInfo.name"
      is-link
      @click="isUpdateNameShow = true"
    />
    <van-cell
      title="性别"
      :value="userInfo.gender === 0 ? '男' : '女'"
      is-link
      @click="isUpdateGenderShow = true"
    />
    <van-cell
      title="生日"
      :value="userInfo.birthday"
      is-link
      @click="isUpdateBirthdayShow = true"
    />

    <!-- 编辑昵称弹层 -->
    <van-popup
      v-model="isUpdateNameShow"
      position="bottom"
      :style="{ height: '100%' }"
    >
      <update-name
        v-if="isUpdateNameShow"
        @close="isUpdateNameShow = false"
        v-model="userInfo.name"
      />
    </van-popup>

    <!-- 编辑性别弹窗 -->
    <van-popup v-model="isUpdateGenderShow" position="bottom">
      <update-gender
        v-if="isUpdateGenderShow"
        @close="isUpdateGenderShow = false"
        v-model="userInfo.gender"
      />
    </van-popup>

    <!-- 编辑生日弹窗 -->
    <van-popup v-model="isUpdateBirthdayShow" position="bottom">
      <update-birthday
        v-if="isUpdateBirthdayShow"
        @close="isUpdateBirthdayShow = false"
        v-model="userInfo.birthday"
      />
    </van-popup>

    <!-- 编辑头像弹窗 -->
    <van-popup
      v-model="isUpdatePhotoShow"
      position="bottom"
      :style="{ height: '100%' }"
    >
      <update-photo
        v-if="isUpdatePhotoShow"
        :img="img"
        @close="isUpdatePhotoShow = false"
        @update-photo="userInfo.photo = $event"
      />
    </van-popup>
  </div>
</template>

<script>
import { getUserProfile } from '@/api/user'

import UpdateName from './components/UpdateName'
import UpdateGender from './components/UpdateGender'
import UpdateBirthday from './components/UpdateBirthday'
import UpdatePhoto from './components/UpdatePhoto'

export default {
  name: 'UserProfile',
  components: { UpdateName, UpdateGender, UpdateBirthday, UpdatePhoto },
  data() {
    return {
      userInfo: {},
      isUpdateNameShow: false,
      isUpdateGenderShow: false,
      isUpdateBirthdayShow: false,
      isUpdatePhotoShow: false,
      img: null // 预览的图片
    }
  },
  created() {
    this.loadUserProfile()
  },
  methods: {
    async loadUserProfile() {
      try {
        const { data: res } = await getUserProfile()
        this.userInfo = res.data
      } catch (error) {
        console.log('数据获取失败')
      }
    },
    onFileChange() {
      // 拿到选择后的文件对象
      const file = this.$refs.file.files[0]
      // 转换为blob
      this.img = window.URL.createObjectURL(file)

      // 展示预览图片弹层
      this.isUpdatePhotoShow = true

      // this.$refs 在二次选择同一个文件的时候  不会重复触发change事件  所以需要把value清空
      this.$refs.file.value = ''
    }
  }
}
</script>

<style lang="less" scoped>
.avatar {
  width: 60px;
  height: 60px;
}

.van-popup {
  background-color: #f5f7f9;
}
</style>
